package com.husawang.husa.lib.base.net;

import java.security.SecureRandom;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by s on 2017/8/8.
 */

public class HusaHttpClient {


	private static HusaHttpClient mHusaHttpClient;
	private OkHttpClient client;

	private HusaHttpClient() {
		client=new OkHttpClient.Builder()
				.sslSocketFactory(createSSLSocketFactory())
				.build();


	}

	public static HusaHttpClient getInstance() {
		if (mHusaHttpClient==null){
			return new HusaHttpClient();
		}
		return mHusaHttpClient;
	}


	public void post(Request request, Callback callback) {
		client.newCall(request).enqueue(callback);
	}


	private static SSLSocketFactory createSSLSocketFactory() {
		SSLSocketFactory ssfFactory = null;

		try {
			SSLContext sc = SSLContext.getInstance("TLS");
			sc.init(null,  new TrustManager[] { new TrustAllCerts() }, new SecureRandom());

			ssfFactory = sc.getSocketFactory();
		} catch (Exception e) {
		}

		return ssfFactory;
	}


}
